<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class PayController extends Controller
{
    public function getPayindex(){


        //此用户的所有收货地址  需要用户id传值
        $res1=DB::table('tbl_home_receiver') -> where('home_userid',session("home_userid")) -> get();
        // dd($res1);
         // var_dump($res1);
        //查询所有支持的付款方式 
        $res2=DB::table('tbl_paymethod') -> get();
        // var_dump($res2);
         //查询所有支持的物流方式 
        $res3=DB::table('tbl_logistics') -> get();
        // dd($res3);
        $paypass = DB::table('tbl_home_user')->where('username',session('homeindex_username'))->select('paypass')->first();

        if(isset($_GET['carid'])){
            //接受加入购物车传过来的值
            $carid1 = $_GET['carid'];
            $totalprice = $_GET['totalprice'];
            // 把接收到的值分割成数组
            $carid = explode(',',$carid1);
            // dd($carid);
            // 遍历数组并查询商品相关信息
            foreach($carid as $k=>$v){
                //根据购物车id  查询商品的具体详情
                $res[] = DB::table('tbl_goodscar') 
                    ->join("tbl_goods",'tbl_goods.id','=','tbl_goodscar.gid') 
                    ->where('tbl_goodscar.id','=',$v) 
                    ->first(); 
            }
            //将购物车结果显示到模板上
             return view('home.order.payindex',['res'=>$res,'res1'=>$res1,'res2'=>$res2,'res3'=>$res3,'totalprice'=>$totalprice,'carid'=>$carid1,'paypass'=>$paypass]);
        }else if(isset($_GET['gid'])){
            $gid = $_GET['gid'];
            $goodsnumber = $_GET['goodsnumber'];
            $res = DB::table('tbl_goods')->where('id',$gid)->get();
            $totalprice = number_format($res[0]['goodsprice']*$goodsnumber,2);
            // dd($totalprice);
            return view('home.order.payindex',['res'=>$res,'gid'=>$gid,'goodsnumber'=>$goodsnumber,'res1'=>$res1,'res2'=>$res2,'res3'=>$res3,'totalprice'=>$totalprice,'paypass'=>$paypass]);
        }
        
        
        
        
    }
    public function getPaysuccess(Request $request){
              // "totalprice" => "40.50"
              // "address" => "北京市-市辖区-昌平区"
              // "addressdetail" => "兄弟连"
              // "user" => "孙鸿 "
              // "phone" => "17705388065"
              // "carid" => "145,146,147,148"
            if(isset($_GET['carid'])){
                    $arr_carid = explode(',',$_GET['carid']);
                    //生成订单(一定要在删除购物车之前)
                    foreach ($arr_carid as $k => $v) {
                        $ordergid[] = DB::table('tbl_goodscar') 
                            ->join("tbl_goods",'tbl_goods.id','=','tbl_goodscar.gid') 
                            ->where('tbl_goodscar.id','=',$v) 
                            ->lists('gid');
                    }
                    $uid = session('home_userid');
                    // dd($uid);
                    $orderstatus = '待发货';
                    $ordernumber = 'EG'.date('YmdHis',time());
                    $ordertotalprice = $_GET['totalprice'];
                    $orderwords = $_GET['orderwords'];
                    $ordertime = date('Y-m-d H:i:s',time());
                    foreach ($ordergid as $k => $v) {
                        foreach ($v as $kk => $vv) {
                            DB::table('tbl_order')->insert([
                                'uid'=>$uid,
                                'gid'=>$vv,
                                'orderstatus'=>$orderstatus,
                                'ordernumber'=>$ordernumber,
                                'ordertotalprice'=>$ordertotalprice,
                                'orderwords'=>$orderwords,
                                'ordertime'=>$ordertime

                                ]);
                        }
                    }
                    //删除购物车中已经付款的商品信息
                    $res = DB::table('tbl_goodscar')->whereIn('id',$arr_carid)->delete();
                    session(['carsum'=>session('carsum')-count(($arr_carid))]);
            }else if(isset($_GET['gid'])){
                //产生订单(不加入购物车直接购买的执行区间)
                 $uid = session('home_userid');
                 $gid = $_GET['gid'];
                // dd($uid);
                $orderstatus = '待发货';
                $ordernumber = 'EG'.date('YmdHis',time());
                $ordertotalprice = $_GET['totalprice'];
                $orderwords = $_GET['orderwords'];
                $ordertime = date('Y-m-d H:i:s',time());
                DB::table('tbl_order')->insert([
                                'uid'=>$uid,
                                'gid'=>$gid,
                                'orderstatus'=>$orderstatus,
                                'ordernumber'=>$ordernumber,
                                'ordertotalprice'=>$ordertotalprice,
                                'orderwords'=>$orderwords,
                                'ordertime'=>$ordertime
                                ]);

            }
    
        return view('home.order.paysuccess',$_GET);
    }





    public function getPayerror(Request $request){
        if(isset($_GET['carid'])){
            $arr_carid = explode(',',$_GET['carid']);
            //生成订单(一定要在删除购物车之前)
            //先根据接受的购物车ID查询出对应的GID
            foreach ($arr_carid as $k => $v) {
                $ordergid[] = DB::table('tbl_goodscar') 
                    ->join("tbl_goods",'tbl_goods.id','=','tbl_goodscar.gid') 
                    ->where('tbl_goodscar.id','=',$v) 
                    ->lists('gid');
            }
            $uid = session('home_userid');
            // dd($uid);
            $orderstatus = '待付款';
            $ordernumber = 'EG'.date('YmdHis',time());
            $ordertotalprice = $_GET['totalprice'];
            $orderwords = $_GET['orderwords'];
            $ordertime = date('Y-m-d H:i:s',time());
            //根据查询出来的GID来生成订单插入数据库
            foreach ($ordergid as $k => $v) {
                foreach ($v as $kk => $vv) {
                    DB::table('tbl_order')->insert([
                        'uid'=>$uid,
                        'gid'=>$vv,
                        'orderstatus'=>$orderstatus,
                        'ordernumber'=>$ordernumber,
                        'ordertotalprice'=>$ordertotalprice,
                        'orderwords'=>$orderwords,
                        'ordertime'=>$ordertime

                        ]);
                }
            }
            //删除购物车中已经付款的商品信息
            $res = DB::table('tbl_goodscar')->whereIn('id',$arr_carid)->delete();
             session(['carsum'=>session('carsum')-count(($arr_carid))]);
        }else if(isset($_GET['gid'])){
            //产生订单(不加入购物车直接购买的执行错误密码区间)
                 $uid = session('home_userid');
                 $gid = $_GET['gid'];
                // dd($uid);
                $orderstatus = '待付款';
                $ordernumber = 'EG'.date('YmdHis',time());
                $ordertotalprice = $_GET['totalprice'];
                $orderwords = $_GET['orderwords'];
                $ordertime = date('Y-m-d H:i:s',time());
                DB::table('tbl_order')->insert([
                                'uid'=>$uid,
                                'gid'=>$gid,
                                'orderstatus'=>$orderstatus,
                                'ordernumber'=>$ordernumber,
                                'ordertotalprice'=>$ordertotalprice,
                                'orderwords'=>$orderwords,
                                'ordertime'=>$ordertime
                                ]);
        }
        
        return redirect('home/order/index');
    }
}
